<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品下单支付页</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f7f7f7;
            text-align: center;
        }
        .container {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .product-info {
            margin-bottom: 20px;
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }
        .product-info h2 {
            margin: 0;
            margin-bottom: 5px;
        }
        .product-info p {
            font-size: 36px;
            font-weight: bold;
            color: red;
            margin: 0;
            margin-top: 20px;
        }
        .order-button {
            display: block;
            width: calc(100% - 40px);
            padding: 10px;
            margin: 10px auto;
            font-size: 16px;
            color: #fff;
            background-color: #007bff;
            border: none;
            cursor: pointer;
            border-radius: 20px;
        }
        .order-button:hover {
            background-color: #0056b3;
        }
        .account-info {
            font-size: 12px;
            text-align: center;
            display: block;
            margin: 10px 0;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="product-info">
        <h2>素描钢琴-码琴栈</h2>

        <img width="350" src="images/sku-keyboard-001.png"/>
        <p>价格：¥1.68</p>
    </div>
    <button id="orderButton" class="order-button">立即下单「支付宝沙箱支付」</button>
    <span class="account-info">测试账号：iyookw2408@sandbox.com 密码：111111 支付：111111</span>
</div>

<script>
function getCookie(name) {
    let cookieArr = document.cookie.split(";");
    for(let i = 0; i < cookieArr.length; i++) {
        let cookiePair = cookieArr[i].split("=");
        if(name == cookiePair[0].trim()) {
            return decodeURIComponent(cookiePair[1]);
        }
    }
    return null;
}

document.getElementById('orderButton').addEventListener('click', function() {
    var userId = getCookie("loginToken");
    if (!userId) {
        window.location.href = "login.html";
        return;
    }

    var productId = "100010090091";
    var url = 'http://127.0.0.1:8080/api/v1/alipay/create_pay_order';

    var requestBody = {
        userId: userId,
        productId: productId
    };

    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(requestBody)
    })
    .then(response => response.json())
    .then(json => {
        if (json.code === "0000") {
            var formHtml = json.data;
            document.body.innerHTML += formHtml; // 将表单添加到页面上
            document.forms[0].submit(); // 自动提交表单
        } else {
            console.error('Error:', json.info);
        }
    })
    .catch(error => console.error('Error:', error));
});
</script>
</body>
</html>

